//////////////////////////////////////////////////////////////
//
//  Description	: PDF2HTML/BMP software
//  Author	: verypdf.com Inc
//  Version	: Ver1.2
//  Platform	: Windows95/98/Me/NT/2000/XP
//  Environment	: Visual C++ 6.0
//  Date	: 2002/04/03
//
//  Copyright(C) 2000--2003 verypdf.com Inc
//      
//  Contact infomation:
//        http://www.verypdf.com/
//        support@verypdf.com
//
//  This program is shareware; you can redistribute and/or 
//  modify it under the terms of the GNU General Public License.
//  See the GNU General Public License for more details.
//
//               Portion base on Xpdf code
//         Copyright 1996-2003 Glyph && Cog, LLC
//////////////////////////////////////////////////////////////

#include <windows.h>
#include "RotateByShear.h"


class CRotateByShearRGB : public CRotateByShear<COLORREF>
{
public: 
	CRotateByShearRGB (ProgressAnbAbortCallBack callback = NULL) : 
      CRotateByShear<COLORREF> (callback) {}
      
      virtual ~CRotateByShearRGB() {}
      
      COLORREF GetRGB (COLORREF *pImage,      // Pointer to image
	      SIZE  sImage,      // Size of image
	      UINT  x,           // X coordinate
	      UINT  y            // Y coordinate
	      )
      {
	      return pImage [x + y * sImage.cx];
      }
      
      // Set RGB value at given pixel coordinates
      void SetRGB (COLORREF  *pImage,   // Pointer to image
	      SIZE       sImage,   // Size of image
	      UINT       x,        // X coordinate
	      UINT       y,        // Y coordinate
	      COLORREF   clr       // New color to set
	      )
      {
	      pImage [x + y * sImage.cx] = clr;
      }
      
      // Create a new bitmap, given a bitmap dimensions
      COLORREF *CreateNewBitmap (SIZE  sImage) // Size of image
      {
	      return new COLORREF [sImage.cx * sImage.cy];
      }
      
      // Create a new bitmap which is an identical copy of the source bitmap
      COLORREF *CopyBitmap (COLORREF *pImage,     // Pointer to source image
	      SIZE      sImage      // Size of source (and destination) image
	      )
      {
	      COLORREF *pDst =  CreateNewBitmap (sImage);
	      if (NULL != pDst)
	      {
		      memcpy (pDst, pImage, sizeof (COLORREF) * sImage.cx * sImage.cy);
	      }
	      return pDst;
      }          
      
      // Destroy a bitmap previously created in call to CreateNewBitmap(..)
      // or to CopyBitmap (...)
      void DestroyBitmap (COLORREF *pImage,   // Pointer to image
	      SIZE                // Size of image
	      )
      {
	      delete [] pImage;
      }
      
};
CRotateByShearRGB *lpRot;

VOID WINAPI DestroyRotateImage(COLORREF *pDst,SIZE sDst)
{ 
	if(lpRot)
	{
		lpRot->DestroyBitmap (pDst, sDst);
		delete lpRot;
		lpRot = NULL;
	}
}

COLORREF * WINAPI RotateImage(COLORREF* lpbit,SIZE m_Size,double Angle,SIZE *sDst,COLORREF bg)
{
	if(lpbit == NULL)
		return NULL;
	lpRot = new CRotateByShearRGB;
	if(lpRot == NULL)
		return NULL;
	COLORREF *pDst = lpRot->AllocAndRotate (
		lpbit,
		m_Size,
		Angle,
		sDst,
		bg
		);
	return pDst;
}
INT	m_nValues[3][3] = {
	{-1,-2, 0},
	{-2, 0, 2},
	{ 0, 2, 1}
};
COLORREF BossFilter(COLORREF *lpDibBits, int x, int y,int m_nHeight,INT m_nDivision,INT m_nBias)
{
	int r = 0;
	int g = 0;
	int b = 0;
	for (int i=0; i<3; i++)
	{
		for (int j=0; j<3; j++)
		{
			COLORREF c = lpDibBits[m_nHeight*(y+j-1)+(x+i-1)];
			r += (m_nValues[i][j] * GetRValue(c));
			g += (m_nValues[i][j] * GetGValue(c));
			b += (m_nValues[i][j] * GetBValue(c));
		}
	}
	r = min(r/m_nDivision + m_nBias, 255);
	g = min(g/m_nDivision + m_nBias, 255);
	b = min(b/m_nDivision + m_nBias, 255);
	r = max(r, 0);
	g = max(g, 0);
	b = max(b, 0);
	
	return RGB(r, g, b);
}
BOOL WINAPI BossImage(COLORREF*lpbit,SIZE m_Size,COLORREF *lpBoss)
{
	if(lpbit == NULL || lpBoss == NULL)
		return FALSE;
	INT m_nHeight = abs(m_Size.cy);
	INT m_nWidth  = m_Size.cx;
	if(m_nHeight <= 0 || m_nWidth <= 0)
		return FALSE;
	for(register int i = 1; i < m_nHeight-1; i++)
	{
		COLORREF *lpBossTemp = &lpBoss[m_nWidth*i];
		for(register int j = 1; j < m_nWidth-1; j++)
		{
			lpBossTemp[j] = BossFilter(lpbit,j,i,m_nWidth,1,180);
		}
	}
	return TRUE;
}
